package easy;

public class Solution_1779 {

    public int nearestValidPoint(int x, int y, int[][] points) {
        int index = -1, min = Integer.MAX_VALUE;
        for (int i = points.length - 1; i >= 0; i--) {
            if (points[i][0] == x || points[i][1] == y) {
                int val = Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y);
                if (val <= min) {
                    index = i;
                    min = val;
                }
            }
        }
        return index;
    }
}
